/*
 * @ : Copyright (c) 2021 Phytium Information Technology, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0.
 *
 * @Date: 2021-04-07 09:53:07
 * @LastEditTime: 2021-07-30 14:18:37
 * @Description:  This files is for general reigster io functions
 *
 * @Modify History:
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */

#ifndef FT_IO_H
#define FT_IO_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "ft_types.h"

    static _INLINE u8 FtIn8(uintptr addr)
    {
        return *(volatile u8 *)addr;
    }

    static _INLINE u16 FtIn16(uintptr addr)
    {
        return *(volatile u16 *)addr;
    }

    static _INLINE u32 FtIn32(uintptr addr)
    {
        return *(volatile u32 *)addr;
    }

    static _INLINE u64 FtIn64(uintptr addr)
    {
        return *(volatile u64 *)addr;
    }

    static _INLINE void FtOut8(uintptr addr, u8 value)
    {
        volatile u8 *local_addr = (volatile u8 *)addr;
        *local_addr = value;
    }

    static _INLINE void FtOut16(uintptr addr, u16 value)
    {
        volatile u16 *local_addr = (volatile u16 *)addr;
        *local_addr = value;
    }

    static _INLINE void FtOut32(uintptr addr, u32 value)
    {
        volatile u32 *local_addr = (volatile u32 *)addr;
        *local_addr = value;
    }

    static _INLINE void FtOut64(uintptr addr, u64 value)
    {
        volatile u64 *local_addr = (volatile u64 *)addr;
        *local_addr = value;
    }

    static _INLINE void FtSetBit32(uintptr addr, u32 value)
    {
        volatile u32 last_value;
        last_value = FtIn32(addr);
        last_value |= value;
        FtOut32(addr, last_value);
    }

    static _INLINE void FtClearBit32(uintptr addr, u32 value)
    {
        volatile u32 last_value;
        last_value = FtIn32(addr);
        last_value &= ~value;
        FtOut32(addr, last_value);
    }

    static _INLINE void FtToggleBit32(uintptr addr, u32 toggle_pos)
    {
        volatile u32 value;
        value = FtIn32(addr);
        value ^= (1 << toggle_pos);
        FtOut32(addr, value);
    }

#define FT_WRITE32(_reg, _val) (*(volatile uint32_t *)&_reg = _val)
#define FT_READ32(_reg) (*(volatile uint32_t *)&_reg)

#ifdef __cplusplus
}
#endif

#endif // !
